<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="pram" :model="pram" :rules="rules" label-width="100px">
      <el-form-item label="类型" prop="type">
        <el-select v-model="pram.type" filterable placeholder="请选择" style="width: 200px">
          <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input type="text" placeholder="标题" v-model="pram.title" class="col" maxlength="50"/>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <Tinymce v-model="pram.content"></Tinymce>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch v-model="pram.status" :active-value="false" :inactive-value="true"></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary"
                   @click="handlerSubmit" v-has-permi="['admin:article:save']">保存
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import Tinymce from "@/components/Tinymce/index.vue";
import {save} from "@/api/article";

export default {
  components: {Tinymce},
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return {rules: []}
      }
    },
  },
  data() {
    return {
      pram: {
        id: null,
        title: '',
        content: '',
        status: 1,
        type: null,
      },
      rules: {
        type: [{required: true, message: '请选择类型', trigger: ['blur']}],
        title: [{required: true, message: '请上传文件', trigger: ['blur']}],
        content: [{required: true, message: '请上传文件', trigger: ['blur']}],
        status: [{required: true, message: '请上传文件', trigger: ['blur']}],
      },
      roles: [],
      saveLoading: false,
      formLoading: false,
      typeList: [{id: 1, name: "巡察公告"}, {id: 2, name: "巡察动态"}, {id: 3, name: "反映须知"}]
    }
  },
  mounted() {
    this.getDetail();
  },
  methods: {
    getDetail() {
      if (this.isCreate !== 1) return //编辑
      const {title, content, status, id, type} = this.editData;
      this.pram.id = id
      this.pram.type = type
      this.pram.title = title;
      this.pram.content = content;
      this.pram.status = status
    },
    handlerSubmit() {
      if (this.saveLoading || this.formLoading) {
        return;
      }
      this.$refs.pram.validate(valid => {
        if (!valid) return;
        this.saveLoading = true;
        save(this.pram).then(() => {
          this.$message.success('保存成功');
          this.close();
        }).finally(() => {
          this.saveLoading = false
        })
      })
    },
    close() {
      this.$emit('hideEditDialog')
    }
  }
}
</script>
